package com.ego.product.web;

import com.ego.common.constant.SystemConstant;
import com.ego.common.to.member.MemberTo;
import com.ego.common.util.Result;
import com.ego.product.feign.MemberFeign;
import com.ego.product.feign.OrderFeign;
import com.ego.product.interceptor.LoginInterceptor;
import com.ego.product.vo.order.WebParamVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @program: ego
 * @description: 会员
 * @author: KamTang
 * @create: 2022-12-27 11:01
 **/
@Controller
@RequestMapping("/member")
@Slf4j
public class MemberController {

    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private MemberFeign memberFeign;

    @GetMapping("/order")
    public String order(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                        @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize,
                        Model model) {
        MemberTo memberTo = LoginInterceptor.threadLocal.get();
        WebParamVo param = new WebParamVo();
        param.setMemberId(memberTo.getId());
        param.setPageNum(pageNum);
        param.setPageSize(pageSize);
        Result pageResult = orderFeign.page(param);
        model.addAttribute("orderPageResult", pageResult);
        return "my-order";

    }

    @GetMapping("/info")
    public String info() {
        return "personal";
    }

    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody MemberTo to) {
        Result result = memberFeign.update(to);
        if (result.getCode() == SystemConstant.Status.SUCCESS.getCode()) {
            return Result.ok();
        } else {
            return Result.error(SystemConstant.Status.FEIGN_EXCEPTION.getCode(), SystemConstant.Status.FEIGN_EXCEPTION.getMsg());
        }
    }
}
